﻿

using Application.Share.Event;
using Application.Share.Model;
using Prism.Commands;
using Prism.Events;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System.Windows.Input;

namespace Application.Login.ViewModels
{
    internal class LoginViewModel : ReactiveObject
    {
        private IEventAggregator EventAggregator { get; }
        public string Title { get; }
        public string Version => System.Windows.Application.ResourceAssembly?.GetName()?.Version?.ToString() ?? string.Empty;
        [Reactive]
        public CurrentUser CurrentUser { get; set; } = new()
        {
            UserName = "admin",
            Password = "123456"
        };
        public ICommand LoginCommand { get; set; }

        public LoginViewModel(IEventAggregator eventAggregator)
        {
            EventAggregator = eventAggregator;
            Title = "欢迎使用本系统";
            CurrentUser = new CurrentUser();
            LoginCommand = new DelegateCommand(OnLogin);
        }

        private void OnLogin()
        {

            if (CurrentUser.UserName == "admin" && CurrentUser.Password == "123456")
            {
                System.Windows.MessageBox.Show("登录成功！");
                EventAggregator.GetEvent<LoginSuccessEvent>().Publish(CurrentUser);//推送登录成功事件
                
            }
            if (string.IsNullOrEmpty(CurrentUser.UserName) || string.IsNullOrEmpty(CurrentUser.Password))
            {
                System.Windows.MessageBox.Show("用户名或密码不能为空");
                return;
            }
           
           
        }
    }
}
